package com.zhengxin.one.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhengxin.one.common.ResultPage;
import com.zhengxin.one.entity.BillBase;
import com.zhengxin.one.entity.entityClass.*;
import com.zhengxin.one.entity.excel.AccumulationExportExcel;
import com.zhengxin.one.entity.excel.CommercialExportExcel;
import com.zhengxin.one.entity.excel.PersonExportExcel;
import com.zhengxin.one.entity.excel.SocietyExportExcel;
import com.zhengxin.one.service.impl.BillServiceipml;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.net.URLEncoder;
import java.util.*;

@RestController
@RequestMapping(value = "shangshebao")
public class PersonFoundController {
    @Autowired
    BillServiceipml billServiceipml;
    //参保人页面接口
    @RequestMapping(value = "personInfo",method = RequestMethod.POST)
    public ResultPage info(@RequestParam String keyword, @RequestParam String company_id, @RequestParam int page, @RequestParam int size){
        if(company_id.isEmpty()){
            //只给她她公司得信息
            return ResultPage.fail("公司id不能为空");
        }
        //先查基础信息
        PageHelper.startPage(page,size);
        List<BillBase> list = billServiceipml.queryBsLists(keyword,company_id,page,size);
        PageInfo pageInfo = new PageInfo(list);
        Page pageinfos =new Page();
        pageinfos.setPageNum(pageInfo.getPageNum());
        pageinfos.setTotal((int)pageInfo.getTotal());
        //返回数据
        List<Object> info = new ArrayList<Object>();
        for (BillBase x:list) {
            //再查各类bill信息
            CBPersion cbPersion = new CBPersion();
            CBSociety cbSociety = new CBSociety();
            CBAccumulation cbAccumulation = new CBAccumulation();
            CBCommercial cbCommercial = new CBCommercial();
            //社保
            Map smap = billServiceipml.billall(x.getId_card(),1);
            if(smap==null){
                cbPersion.setSolist(cbSociety);
            }else{
                String re =(String) smap.get("region");
                String cb =(String) smap.get("cbase");
                Date jo =(Date) smap.get("jointime");
                cbSociety.setRegion(re);
                cbSociety.setCbase(cb);
                cbSociety.setJointime(jo);
            }
            //公积金
            Map amap = billServiceipml.billall(x.getId_card(),2);
            if(amap==null){
                cbPersion.setAclist(cbAccumulation);
            }else{
                String re =(String) amap.get("region");
                String cb =(String) amap.get("cbase");
                Date jo =(Date) amap.get("jointime");
                cbAccumulation.setRegion(re);
                cbAccumulation.setCbase(cb);
                cbAccumulation.setJointime(jo);
            }
            //商保
            Map cmap = billServiceipml.billall(x.getId_card(),3);
            if(cmap==null){
                cbPersion.setColist(cbCommercial);
            }else{
                String pl =(String) cmap.get("plan");
                Date jo =(Date) cmap.get("jointime");
                cbCommercial.setPlan(pl);
                cbCommercial.setJointime(jo);
            }
            //再拼接组合返回
            cbPersion.setId_card(x.getId_card());
            cbPersion.setName(x.getName());
            cbPersion.setPhone(x.getPhone());
            cbPersion.setNationality(x.getNationality());
            cbPersion.setRegion(x.getRegion());
            cbPersion.setResidence_type(x.getResidence_type());
            cbPersion.setSex(x.getSex());
            cbPersion.setSolist(cbSociety);
            cbPersion.setAclist(cbAccumulation);
            cbPersion.setColist(cbCommercial);
            info.add(cbPersion);
        }

        return ResultPage.successp(info,pageinfos);
    }
    //execl导出参保人
    @RequestMapping("exportPersonExcel")
    public void export(HttpServletRequest request, HttpServletResponse response, @RequestParam String keyword, @RequestParam String company_id, @RequestParam int page, @RequestParam int size)throws Exception{
        response.setHeader("content-Type","application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=canbaoren.xlsx");
        response.setCharacterEncoding("UTF-8");
        PageHelper.startPage(page,size);
        List<PersonExportExcel> list = billServiceipml.queryexports(keyword,company_id,page,size);
        PageInfo pageInfo = new PageInfo(list);
        for (PersonExportExcel x:list) {
            //加入商保信息
            List<SocietyExportExcel> societyExportExcel = new LinkedList<SocietyExportExcel>();
            SocietyExportExcel society = new SocietyExportExcel();
            Map so = billServiceipml.billall(x.getId_card(),1);
            if(so!=null){
                society.setCbase((String) so.get("cbase"));
                society.setRegion((String) so.get("region"));
                society.setJointime((Date) so.get("jointime"));
                societyExportExcel.add(society);

            }else {
                society.setCbase("");
                society.setRegion("");
                society.setJointime(null);
                societyExportExcel.add(society);
            }
            x.setSolist(societyExportExcel);
            //加入公积金信息
            List<AccumulationExportExcel> accumulationExportExcels = new LinkedList<AccumulationExportExcel>();
            AccumulationExportExcel accumulation = new AccumulationExportExcel();
            Map ac = billServiceipml.billall(x.getId_card(),2);
            if(ac!=null){
                accumulation.setCbase((String) ac.get("cbase"));
                accumulation.setRegion((String) ac.get("region"));
                accumulation.setJointime((Date) ac.get("jointime"));
                accumulation.setCompany_ratio((String) ac.get("company_ratio"));
                accumulation.setPerson_ratio((String) ac.get("person_ratio"));
                accumulationExportExcels.add(accumulation);
            }else {
                accumulation.setCbase("");
                accumulation.setRegion("");
                accumulation.setJointime(null);
                accumulation.setCompany_ratio("");
                accumulation.setPerson_ratio("");
                accumulationExportExcels.add(accumulation);
            }
            x.setAclist(accumulationExportExcels);
            //加入商保信息
            List<CommercialExportExcel> commercialExportExcels = new LinkedList<CommercialExportExcel>();
            CommercialExportExcel commercial = new CommercialExportExcel();
            Map co = billServiceipml.billall(x.getId_card(),2);
            if(co!=null){
                commercial.setPlan((String) co.get("plan"));
                commercial.setJointime((Date) co.get("jointime"));
                commercialExportExcels.add(commercial);
            }else {
                commercial.setPlan("");
                commercial.setJointime(null);
                commercialExportExcels.add(commercial);
            }
            x.setColist(commercialExportExcels);
        }
        //设置Excel的描述文件
        ExportParams exportParams = new ExportParams("产保人", "产保人信息" , ExcelType.XSSF);
        //进行导出的基本操作
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, PersonExportExcel.class, list);
        //进行输出流
        workbook.write(response.getOutputStream());
        //关流
        workbook.close();
    }
}
